package cn.edu.pku.ss.situationawareperceiver;

import java.util.HashMap;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import cn.edu.pku.ss.situationawareperceiver.ContextConstants.ContextType;

public class ContextHandler extends DefaultHandler{
	
	private ContextType contextType;
	private ContextTruthValue truthValue;
	private HashMap<ContextType, ContextTruthValue> truthValueMapper;
	
	private final int CONTEXTTYPE = 1;
	private final int VALUE = 2;
	private final int DISCRETE = 3;
	private int currentState = 0;
	
	public static HashMap<String, ContextType> contextTypeMapper;
	
	public HashMap<ContextType, ContextTruthValue> getTruthValueMapper()
	{
		return truthValueMapper;
	}
	
	public ContextHandler() 
	{
		contextTypeMapper = new HashMap<String, ContextConstants.ContextType>();
		truthValueMapper = new HashMap<ContextConstants.ContextType, ContextTruthValue>();
		
		contextTypeMapper.put("FLOOR_WEIGHT", ContextType.FLOOR_WEIGHT);
		contextTypeMapper.put("HAS_COFFEE_CUP", ContextType.HAS_COFFEE_CUP);
		contextTypeMapper.put("COFFEE_TEMPERATURE", ContextType.COFFEE_TEMPERATURE);
		contextTypeMapper.put("IS_MEMBER", ContextType.IS_MEMBER);
		contextTypeMapper.put("LOCATION", ContextType.LOCATION);
		contextTypeMapper.put("NUMBER_OF_PEOPLE_IN_ROOM", ContextType.NUMBER_OF_PEOPLE_IN_ROOM);
		contextTypeMapper.put("DIARY", ContextType.DIARY);
		contextTypeMapper.put("ROOM_LIGHT", ContextType.ROOM_LIGHT);
		contextTypeMapper.put("PROJECTOR", ContextType.PROJECTOR);
		contextTypeMapper.put("SOUND_METER", ContextType.SOUND_METER);
		contextTypeMapper.put("SOON_TO_DINNER", ContextType.SOON_TO_DINNER);
		contextTypeMapper.put("IN_BED", ContextType.IN_BED);
		contextTypeMapper.put("AT_NIGHT", ContextType.AT_NIGHT);
		contextTypeMapper.put("HOLD_READING_EQUIPMENT", ContextType.HOLD_READING_EQUIPMENT);
		contextTypeMapper.put("TV", ContextType.TV);
		contextTypeMapper.put("IN_SEAT", ContextType.IN_SEAT);
		contextTypeMapper.put("IN_FRIENDS_LIST", ContextType.IN_FRIENDS_LIST);
		contextTypeMapper.put("IN_KITCHEN", ContextType.IN_KITCHEN);
		contextTypeMapper.put("DIARY_FOR_TRAVEL", ContextType.DIARY_FOR_TRAVEL);
	}
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		if(localName.equals("context"))
		{
			truthValue = new ContextTruthValue();
			return;
		}
		if(localName.equals("contextType"))
		{
			currentState = CONTEXTTYPE;
			return;
		}
		if(localName.equals("value"))
		{
			currentState = VALUE;
			return;
		}
		if(localName.equals("discrete"))
		{
			currentState = DISCRETE;
			return;
		}
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		String content = new String(ch, start, length);
		switch(currentState)
		{
		case CONTEXTTYPE:
			contextType = contextTypeMapper.get(content);
			currentState = 0;
			break;
		case VALUE:
			String[] values = content.split(",");
			truthValue.lValue = values[0];
			truthValue.rValue = values[1];
			currentState = 0;
			break;
		case DISCRETE:
			if(content.equals("true"))
				truthValue.isDiscrete = true;
			else
				truthValue.isDiscrete = false;
			currentState = 0;
			break;
		}
	}
	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		if(localName.equals("context"))
		{
			truthValueMapper.put(contextType, truthValue);
			return;
		}
	}
}
